package com.sky.service.common.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.common.SetmealDishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.common.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //将套餐数据插入数据库
        setmealMapper.addSetmeal(setmeal);
        //获取套餐的id
        Long id = setmeal.getId();
        //获取套餐的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //保存套餐和菜品的关联关系
        boolean result = setmealDishMapper.addSetmealDish(setmealDishes);
        if (result){
            log.info("套餐和菜品的关系保存成功");
        }else {
            log.info("套餐和菜品的关系保存失败");
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),10);
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        log.info("分页查询结果：{}",page);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        Boolean result = setmealMapper.deleteByIds(ids);
        Boolean result1 = setmealDishMapper.deleteByIds(ids);
        if (result && result1){
            log.info("套餐表和关系表 删除成功");
        }else {
            log.info("套餐表和关系表 删除失败");
        }
    }

    /**
     * 根据套餐id查询套餐数据
     * @param setmealId
     * @return
     */
    @Override
    public SetmealVO findById(Long setmealId) {
        //查询套餐数据
        SetmealVO setmealVO = setmealMapper.findById(setmealId);
        log.info("根据id：{} 查询到的套餐数据：{}",setmealId,setmealVO);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public Integer updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐数据
        Integer row = setmealMapper.updateSetmeal(setmeal);
        log.info("修改套餐数据：{},影响了{}行数据",setmeal, row);
        //修改套餐和菜品的关联关系
        //先删除套餐和菜品的关联关系
        List<Long> ids = new ArrayList<>();
        ids.add(setmealDTO.getId());
        System.out.println(ids);
        //给套餐中的菜品列表的套餐id赋值
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        boolean result = setmealDishMapper.deleteByIds(Arrays.asList(setmealDTO.getId()));
        System.out.println(result);
        //再插入新的关联关系

        Boolean result1 = setmealDishMapper.addSetmealDish(setmealDTO.getSetmealDishes());
        System.out.println(result1);
        return row;
    }

    /**
     * 修改套餐起售停售状态
     * @param status 1起售 0停售
     * @param setmealId 套餐id
     */
    @Override
    public void startOrStop(Integer status, Long setmealId) {
        Setmeal setmeal = Setmeal.builder()
                .status(status == StatusConstant.DISABLE ? StatusConstant.DISABLE : StatusConstant.ENABLE)
                .id(setmealId)
                .build();
                Integer row = setmealMapper.updateSetmeal(setmeal);
                log.info("修改套餐状态：{}",status);
                log.info("修改套餐状态 影响的行数：{}",row);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {

        List<DishItemVO> dishItemVOS = setmealMapper.getDishItemBySetmealId(id);
        //查询菜品口味

        return dishItemVOS;
    }


}
